﻿using RepositoryDefinition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryImplementation
{
    public class UserRepository:IUserRepository
    {
        public int GetUserCountByUsernameAndPassword(ForumDomainModels.User user)
        {
            ForumDAL.UserDAL userContext = new ForumDAL.UserDAL();
            int userCount = userContext.GetUserCountWithSpecifiedUsernameAndPassword(user);
            return userCount;
        }

        public int GetUserCountByUsername(ForumDomainModels.User user)
        {
            ForumDAL.UserDAL userContext = new ForumDAL.UserDAL();
            int userCount = userContext.GetUserCountWithSpecifiedUsername(user);
            return userCount;
        }

        public ForumDomainModels.User GetUserByUsernameAndPassword(ForumDomainModels.User user)
        {
            try
            {
                ForumDAL.UserDAL userContext = new ForumDAL.UserDAL();
                ForumDomainModels.User retrievedUser = userContext.GetUserWithSpecifiedUsernameAndPassword(user);
                return retrievedUser;
            }
            catch (ForumDAL.CustomExceptions.NoRowsRetrieved e)
            {
                return null;
            }
            
        }

        public void InsertUser(ForumDomainModels.User user)
        {
            try
            {
                ForumDAL.UserDAL userContext = new ForumDAL.UserDAL();
                userContext.InsertUser(user);
            }
            catch (ForumDAL.CustomExceptions.NotInsertedRowsInDatabase e)
            {
                throw new ForumDAL.CustomExceptions.NotInsertedRowsInDatabase();
            }
            
        }


        public ForumDomainModels.User GetUserById(ForumDomainModels.User creatorPost)
        {
            try
            {
                ForumDAL.UserDAL userContext = new ForumDAL.UserDAL();
                ForumDomainModels.User user = userContext.GetUserById(creatorPost);
                return user;
            }
            catch (ForumDAL.CustomExceptions.NoRowsRetrieved e)
            {
                return null;
            }
        }
    }
}
